package com.woniuxy.user.controller;

import com.itextpdf.text.DocumentException;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.woniuxy.commons.dto.*;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.exception.user.UserNotExitException;
import com.woniuxy.commons.service.RemoteCourseService;

import com.woniuxy.commons.service.RemoteExamService;
import com.woniuxy.commons.service.RemoteLearnMapService;
import com.woniuxy.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.plugin.dom.core.Element;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


/**
 * @author 吴毛伟
 * @date:2020/7/16 10:08
 * @description:
 */
@RestController
//@RequestMapping("/user")
public class UserController {
    @Autowired
    private RemoteCourseService remoteCourseService;
    @Autowired
    private RemoteExamService remoteExamService;
    @Autowired
    private RemoteLearnMapService remoteLearnMapService;

    @Autowired
    private UserService userService;

    /**
     * @date:9:40 2020/7/20
     * @description:课程分页查询
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @RequestMapping("/selectAll")
    public ResponseEntity<PageBean<CourseDto>> selectAll(@RequestBody CourseDto courseDto) {
        return remoteCourseService.queryCoursesByCondition(courseDto);
    }

    /**
     * @date:16:12 2020/8/4
     * @description:查询所有用户
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/selectAllUser")
    public ResponseEntity<List<User>> selectAllUser() {
        List<User> users = userService.selectAllUser();
        ResponseEntity responseEntity = new ResponseEntity();
        return responseEntity.setCode(200).setData(users);
    }

    /**
     * @date:10:02 2020/7/30
     * @description:课程类别查询
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/courseCategory/findallCategories")
    public ResponseEntity<List<CourseCategory>> findAllCategories() {
        return remoteCourseService.findAllCategories();
    }

    /**
     * @date:11:24 2020/7/29
     * @description:通过用户名查询用户
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/selectUserByUname/{uname}")
    public ResponseEntity<User> selectUserByUname(@PathVariable("uname") String uname) {
        ResponseEntity<User> responseEntity = new ResponseEntity<>();
        try {
            User user = userService.selectUserByUname(uname);
            return new ResponseEntity<>(user);
        } catch (UserNotExitException e) {
            return responseEntity.setCode(300).setMessage("用户不存在");
        }
    }

    /**
     * @date:10:39 2020/7/28
     * @description:通过ID查看考试
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/find/{id}")
    public Exam findById(@PathVariable("id") int id) {
        return remoteExamService.findById(id);
    }

    /**
     * @date:10:48 2020/7/28
     * @description:通过ID获取试卷
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @RequestMapping("/find/{paperId}")
    public ResponseEntity<Paper> findPaperByPaperId(@PathVariable("paperId") int paperId) {
        return remoteExamService.findPaperByPaperId(paperId);
    }

    /**
     * @date:10:40 2020/7/28
     * @description:通过试卷id获取相应的试卷结构
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/getStructure/{paperId}")
    public ResponseEntity<PaperStructure> findPaperStructureByPaperId(@PathVariable("paperId") int paperId) {
        return remoteExamService.findPaperStructureByPaperId(paperId);
    }

    /**
     * @date:10:08 2020/7/29
     * @description:查看试卷
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/fullPaper/{paperId}")
    public ResponseEntity<PaperDto> getFullPaper(@PathVariable("paperId") int paperId) {
        return remoteExamService.getFullPaper(paperId);
    }

    /**
     * @date:10:42 2020/7/28
     * @description:打印試卷
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/printPaper/{paperId}")
    public ResponseEntity<String> printPaper(@PathVariable("paperId") int paperId) throws IOException, DocumentException {
        return remoteExamService.printPaper(paperId);
    }

    /**
     * @date:11:21 2020/7/28
     * @description:请求考试
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @PostMapping("/getExam")
    public ResponseEntity<Void> getExam(HttpSession httpSession) {
        //获取当前登录账户
        User user = (User) httpSession.getAttribute("user");
        //申请考试传入用户id
        int code = userService.getExam(user.getUid());
        if (code == 1) {
            return ResponseEntity.SUCCESS_RESPONSE.setCode(200);
        }
        return ResponseEntity.SUCCESS_RESPONSE.setCode(code);
    }

    /**
     * @date:15:04 2020/7/28
     * @description:成绩查询
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/getScore")
    public ResponseEntity<Integer> selectScore(HttpSession httpSession) {
        //获取当前登录账户
        User user = (User) httpSession.getAttribute("user");
        //通过用户ID查询成绩
        int data = userService.selectScore(user.getUid());
        return new ResponseEntity<>(data);
    }

    /**
     * @date:15:15 2020/7/28
     * @description:学习地图闯关判断
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/getStudeyMap/{id}")
    public ResponseEntity<LearnMap> getStudyMap(@PathVariable("id") int id, HttpSession httpSession) {
        //获取当前登录账户
        User user = (User) httpSession.getAttribute("user");

        return null;
    }

    /**
     * @date:15:56 2020/7/28
     * @description:添加查看课件的学习记录
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @PostMapping("insertMap/{id}")
    public ResponseEntity<Void> insertMap(@PathVariable("id") int id, HttpSession httpSession) {
        //获取当前登录账户
        User user = (User) httpSession.getAttribute("user");
        //首先判断是否已经添加过了 如果没有 则添加
        int row = userService.insertMap(user.getUid(), id);
        if (row == 1) {
            //则存在该浏览记录了 就不用添加
            return ResponseEntity.SUCCESS_RESPONSE.setCode(300);
        }
        return ResponseEntity.SUCCESS_RESPONSE.setCode(200);
    }

    /**
     * @date:20:45 2020/7/30
     * @description:
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @PostMapping("/findLearnMapsByPage")
    public ResponseEntity<PageBean<LearnMapDto>> findLearnMapsByPage(@RequestBody LearnMapDto learnMapDto) {
        return remoteLearnMapService.findLearnMapsByPage(learnMapDto);
    }

    /**
     * @date:10:41 2020/7/31
     * @description:通过cid查询章节
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/findChapterByCid")
    public ResponseEntity<CourseChapter> findChapterByCid(@PathVariable("cid") CourseChapter courseChapter) {
        ResponseEntity<CourseChapter> responseEntity = new ResponseEntity<>();
        CourseChapter data = userService.findChapterByCid(courseChapter.getCid());
        return responseEntity.setData(data).setCode(200);
    }

    /**
     * @date:16:23 2020/8/1
     * @description:接收答案
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @PostMapping("/insertAnswers")
    public ResponseEntity<ExamAnswer> insertAnswers(@RequestBody ExamAnswer examAnswer, HttpSession httpSession) {
        ResponseEntity<ExamAnswer> responseEntity = new ResponseEntity<>();
        //获取当前登录账户
        User user = (User) httpSession.getAttribute("user");
        examAnswer.setUserId(1);
        int raw = userService.insertAnswers(examAnswer);
        if (raw > 0) {
            return responseEntity.setCode(200);
        }
        return responseEntity.setCode(300);
    }

    /**
     * @date:9:46 2020/8/3
     * @description:查询自己选择的答案
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/getAnswer/{examId}")
    public ResponseEntity<List<ExamAnswer>> getAnswer(@PathVariable("examId") int examId, HttpSession httpSession) {
        ResponseEntity<List<ExamAnswer>> responseEntity = new ResponseEntity<>();
        //获取当前登录账户
        /*User user = (User) httpSession.getAttribute("user");*/
        User user = new User();
        user.setUid(1);
        List<ExamAnswer> examAnswer = userService.getAnswer(examId, user.getUid());
        if (examAnswer != null) {
            return responseEntity.setCode(200).setData(examAnswer);
        }
        return responseEntity.setCode(300);
    }

    /**
     * @date:14:24 2020/8/3
     * @description:学习地图详情查询
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @PostMapping("/findLearnMapDetails")
    public ResponseEntity<List<LearnMapDetailDto>> findLearnMapDetailsByLmid(@RequestBody LearnMap learnMap) {
        return remoteLearnMapService.findLearnMapDetailsByLmid(learnMap);
    }

    /**
     * @date:16:04 2020/8/3
     * @description:添加考试信息
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @PostMapping("/insertByExamIdAndUserId/{paperId}")
    public ResponseEntity<ExamHistory> insertByExamIdAndUserId(@PathVariable("paperId") int paperId) {
        ResponseEntity<ExamHistory> responseEntity = new ResponseEntity<>();
        /*User user = (User) httpSession.getAttribute("user");*/
        User user = new User();
        user.setUid(1);
        int row = userService.insertByExamIdAndUserId(paperId, user.getUid());
        if (row > 0) {
            return responseEntity.setCode(200);
        }
        return responseEntity.setCode(300);
    }

    /**
     * @date:15:54 2020/8/3
     * @description:考试状态查询
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/findSatusByExamIdAndUserId/{paperId}")
    public ResponseEntity<ExamHistory> findSatusByExamIdAndUserId(@PathVariable("paperId") int paperId) {
        ResponseEntity<ExamHistory> responseEntity = new ResponseEntity<>();
        /*User user = (User) httpSession.getAttribute("user");*/
        User user = new User();
        user.setUid(1);
        ExamHistory examHistory = userService.findSatusByExamIdAndUserId(paperId, user.getUid());
        if (examHistory != null) {
            return responseEntity.setCode(200).setData(examHistory);
        }
        return responseEntity.setCode(300);
    }

    /**
     * @date:16:14 2020/8/3
     * @description:修改考试状态
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @PostMapping("/updateStatu/{paperId}")
    public ResponseEntity<ExamHistory> updateStatus1(@PathVariable("paperId") int paperId) {
        ResponseEntity<ExamHistory> responseEntity = new ResponseEntity<>();
        /*User user = (User) httpSession.getAttribute("user");*/
        User user = new User();
        user.setUid(1);
        int row = userService.updateStatus1(paperId, user.getUid());
        if (row > 0) {
            return responseEntity.setCode(200);
        }
        return responseEntity.setCode(300);
    }

    @PostMapping("/updateStatus/{paperId}")
    public ResponseEntity<ExamHistory> updateStatus2(@PathVariable("paperId") int paperId) {
        ResponseEntity<ExamHistory> responseEntity = new ResponseEntity<>();
        /*User user = (User) httpSession.getAttribute("user");*/
        User user = new User();
        user.setUid(1);
        int row = userService.updateStatus2(paperId, user.getUid());
        if (row > 0) {
            return responseEntity.setCode(200);
        }
        return responseEntity.setCode(300);
    }

    /**
     * @date:9:32 2020/8/4
     * @description:
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/findChapterByCid/{cid}")
    public ResponseEntity<List<CourseChapter>> findChapterByCid(@PathVariable("cid") int cid) {
        return remoteCourseService.findChapterByCid(cid);
    }

    @GetMapping("/findCoursewares/{cpid}")
    public ResponseEntity<List<Courseware>> findCoursewaresByCpid(@PathVariable("cpid") int cpid) {
        return remoteCourseService.findCoursewaresByCpid(cpid);
    }

    /**
     * @date:10:14 2020/8/4
     * @description:查询所有老师
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @GetMapping("/allTeachers")
    public ResponseEntity<List<Teacher>> findAllTeachers() {
        return remoteCourseService.findAllTeachers();
    }

    @PostMapping("/findRouteDetails")
    public ResponseEntity<List<RouteDetailDto>> findRouteDetailsByRouteId(@RequestBody Route route) {
        return remoteLearnMapService.findRouteDetailsByRouteId(route);
    }

    /**
     * @date:15:29 2020/8/4
     * @description:
     * @params:
     * @return:
     * @author 吴毛伟
     */
    @RequestMapping("/findCoursewareByCwid")
    public ResponseEntity findCoursewareByCwid(@RequestBody Courseware courseware) {
        return remoteCourseService.findCoursewareByCwid(courseware);
    }
}
